package cn.o.generate.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.o.generate.dao.GeneratorDao;
import cn.o.generate.GenUtils;
import cn.o.generate.service.GeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器-实现层
 *
 */
@Service
public class GeneratorServiceImpl implements GeneratorService {
	@Autowired
	private GeneratorDao generatorDao;
    @Override
	public List<Map<String, Object>> queryList() {
		List<Map<String, Object>> list = generatorDao.queryList();

		return list;
	}

    @Override
	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = generatorDao.queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = generatorDao.queryColumns(tableName);
			//生成代码
			GenUtils.generatorCode(table, columns, zip);
		}
		//关闭
        IoUtil.close(zip);
		return outputStream.toByteArray();
	}
}
